package com.zmn.plat.admin.dialect.number;

import com.zmn.common.utils.number.NumberUtil;
import org.thymeleaf.context.IExpressionContext;
import org.thymeleaf.expression.IExpressionObjectFactory;

import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @uthor nowind
 * @since 2018/10/13 08:52
 */
public class NumberExpressionFactory implements IExpressionObjectFactory {

    public static final String NUMBER_UTIL_EXPRESSION_OBJECT_NAME = "numberUtil";

    private static final NumberUtil NUMBER_UTIL = new NumberUtil();

    public static final Set<String> ALL_EXPRESSION_OBJECT_NAMES;


    public NumberExpressionFactory() {
        super();
    }

    static {
        final Set<String> allExpressionObjectNames = new LinkedHashSet<String>();
        allExpressionObjectNames.add(NUMBER_UTIL_EXPRESSION_OBJECT_NAME);
        ALL_EXPRESSION_OBJECT_NAMES = Collections.unmodifiableSet(allExpressionObjectNames);
    }

    /**
     * 返回该工厂类能创建的工具类对象的集合
     */
    @Override
    public Set<String> getAllExpressionObjectNames() {
        return ALL_EXPRESSION_OBJECT_NAMES;
    }

    /**
     * 根据表达式的名称,创建工具类对象
     */
    @Override
    public Object buildObject(IExpressionContext iExpressionContext, String s) {
        return NUMBER_UTIL_EXPRESSION_OBJECT_NAME.equals(s) ? NUMBER_UTIL : null;
    }

    /**
     * 返回该工具对象是否可缓存
     */
    @Override
    public boolean isCacheable(String s) {
        return s != null && s.equals(NUMBER_UTIL_EXPRESSION_OBJECT_NAME);
    }
}
